package org.inter.fsb.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.inter.fsb.entity.User;
import org.inter.fsb.service.CodeService;
import org.inter.fsb.service.IUserService;
import org.inter.fsb.utils.DESUtil;
import org.inter.fsb.utils.ProxyResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *
 * </p>
 *
 * @author syg
 * @since 2022-11-25
 */
@Api(tags = "用户信息")
@Controller
@RequestMapping("/inter/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService service;
    @Autowired
    private CodeService codeService;

    @ApiOperation("登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public @ResponseBody
    ProxyResponse login(@RequestBody User user) throws Exception {
        Map login = service.login(user);
        return toJson(login);
    }

    @ApiOperation("发送验证码")
    @RequestMapping(value = "/smsSend", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse smsSend(@RequestParam(value = "phoneNUmber", required = true) String phoneNumber) {
        service.sms(phoneNumber);
        return toJson(null);
    }

    @ApiOperation("实名认证")
    @RequestMapping(value = "/namer", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse nameR(
            @RequestParam(value = "username", required = true) String username,
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "number", required = true) String number) throws Exception {
        service.iDcardRe(username,name,number);
        return toJson(null);
    }

    @ApiOperation("查询一个功能")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse get(User entity) {
        Wrapper<User> wrapper = new QueryWrapper<User>(entity);
        User result = service.getOne(wrapper);
        return toJson(result);
    }

    @ApiOperation("查询功能")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse list(User entity) {
        Wrapper<User> wrapper = new QueryWrapper<User>(entity);
        List<User> result = service.list(wrapper);
        return toJson(result);
    }

    @ApiOperation("分页查询功能")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse page(User entity,
                       @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                       @RequestParam(name = "size", required = false, defaultValue = "10") Integer size) {

        Page<User> pageable = new Page<>(page, size);
        Wrapper<User> wrapper = new QueryWrapper<User>(entity);
        Page<User> result = service.page(pageable, wrapper);
        return toJson(result);
    }

    @ApiOperation("根据ID查询功能")
    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse findOne(User entity) {
        Wrapper<User> wrapper = new QueryWrapper<User>(entity);
        User result = service.getOne(wrapper);
        return toJson(result);
    }


    @ApiOperation("新建或更新数据功能")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public @ResponseBody ProxyResponse save(@RequestBody User entity) throws Exception {
        boolean result = service.saveOrUpdate(entity);
        if (result) {
            return toJson("更新成功");
        }
        throw new Exception("更新失败");
    }

    @ApiOperation("新建或更新数据功能")
    @RequestMapping(value = "/save11", method = RequestMethod.POST)
    public @ResponseBody ProxyResponse save11(@RequestBody User entity) throws Exception {
        entity.setPassword(DESUtil.getEncryptString(entity.getPassword()));
        entity.setDeptName("临时用户部");
        entity.setDeptId(5);
        entity.setRoleId(2);
        entity.setRoleName("普通用户");
        boolean result = service.saveOrUpdate(entity);
        if (result) {
            return toJson("更新成功");
        }
        throw new Exception("更新失败");
    }

    @ApiOperation("删除数据功能")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public @ResponseBody ProxyResponse remove(@RequestParam("id") int id) throws Exception {

        return toJson(null);
    }

    @ApiOperation("重置密码")
    @RequestMapping(value = "/reset", method = RequestMethod.DELETE)
    public @ResponseBody ProxyResponse reset(@RequestParam("id") int id) throws Exception {
        User byId = service.getById(id);
        byId.setPassword("123456");
        service.saveOrUpdate(byId);
        return toJson(null);
    }

    @ApiOperation("用户管理保存用户信息")
    @RequestMapping(value = "/saveUser", method = RequestMethod.POST)
    public @ResponseBody ProxyResponse saveUser(@RequestBody User user) throws Exception {

        return toJson(null);
    }

    @ApiOperation("充值")
    @RequestMapping(value = "/chongzhi", method = RequestMethod.POST)
    public @ResponseBody ProxyResponse chongzhi(@RequestBody User user) throws Exception {
        service.chongzhi(user.getUsername(), user.getJine());
        return toJson(null);
    }

}
